<script>
import {
  Scene,
  PerspectiveCamera,
  WebGLRenderer,
  Vector3,
  LineBasicMaterial,
  Geometry,
  Line,
  Color,
  LineSegments
} from 'three'

import Detector from 'three/examples/js/Detector'
export default {
  template: '<div style="width: 100%; height: 100%"></div>',
  data() {
    return {
      scene: null,
      camera: null,
      renderer: null,
      cube: null,
      ele: null
    }
  },
  mounted() {
    this.ele = this.$el

    if (Detector.webgl) {
      this.init()
    } else {
      const warning = Detector.getWebGLErrorMessage()
      this.ele.appendChild(warning)
    }
  },
  methods: {
    init() {
      const width = this.ele.clientWidth
      const height = this.ele.clientHeight
      this.renderer = new WebGLRenderer({
        antialias : true
      })
      this.renderer.setSize(width, height)
      this.ele.appendChild(this.renderer.domElement)
      this.renderer.setClearColor(0xffffff, 1)

      this.camera = new PerspectiveCamera(45, width / height, 1, 10000)
      this.camera.position.set(0, 1000, 0)
      this.camera.up.set(0, 0, 1)
      this.camera.lookAt(new Vector3(0, 0, 0))

      this.scene = new Scene()

      /* const light = new DirectionalLight(0xff0000, 1, 0)
      light.position.set(100, 100, 200)
      this.scene.add(light) */

      const material = new LineBasicMaterial({
        vertexColors: true
      })
      const geometry = new Geometry()
      const color1 = new Color(0x444444)
      const color2 = new Color(0xFF0000)
      const p1 = new Vector3(-100, 0, 100)
      const p2 = new Vector3(100, 0, -100)
      geometry.vertices.push(p1)
      geometry.vertices.push(p2)
      geometry.colors.push(color1, color2)

      const line = new Line(geometry, material, LineSegments)
      this.scene.add(line)
      this.renderer.render(this.scene, this.camera)
    }
  }
}
</script>